<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

function product_sku_add_page() {
    return drupal_get_form('product_sku_edit_form');
}

function product_sku_edit_page($id) {
    return drupal_get_form('product_sku_edit_form', $id);
}

function product_sku_edit_form($form, &$form_state) {
    $id = isset($form_state['build_info']['args'][0]) ? $form_state['build_info']['args'][0] : NULL;
    if (!empty($id)) {
        $record = nonghua_data_load('product_sku', 'product_sku_id', $id);
        if (empty($record)) {
            drupal_set_message(t('product_sku数据不存在！'), 'status', FALSE);
            return $form;
        }
        $form_state['update'] = true;
        $form_state['id'] = $id;
    }else{
        $form_state['insert'] = true;
    }
//    // 判断用户角色
//    if(!nonghua_user_access('product_sku', 'view', $id, !empty($record) ? $record->author_uid:0, 0) ){
//        drupal_set_message(t('您没有权限查看本条数据！'), 'status', FALSE);
//        return $form;
//    }
    _insert_brand_selection_form($form,!empty($record) ? $record->brand_id:'');
    $form['variety_name'] = array(
        '#type' => 'textfield',
        '#title' => '化肥品种',
        '#size' => 32,
        '#maxlength' => 32,
        '#required' => TRUE,
        '#default_value' => !empty($record) ? $record->variety_name:'',
    );
    $form['proportion_one'] = array(
        '#type' => 'textfield',
        '#title' => '比例',
        '#size' => 32,
        '#maxlength' => 32,
        '#required' => TRUE,
        '#default_value' => !empty($record) ? explode('-',$record->proportion)[0]:'',
    );
//    $form['one_markup']['#markup'] = "<span style='float: left;'>-</span>";
    $form['proportion_two'] = array(
        '#type' => 'textfield',
        '#size' => 32,
        '#maxlength' => 32,
        '#required' => TRUE,
        '#default_value' => !empty($record) ? explode('-',$record->proportion)[1]:'',
    );
//    $form['two_markup']['#markup'] = "<span style='float: left;'>-</span>";
    $form['proportion_three'] = array(
        '#type' => 'textfield',
        '#size' => 32,
        '#maxlength' => 32,
        '#required' => TRUE,
        '#default_value' => !empty($record) ? explode('-',$record->proportion)[2]:'',
    );
    $form['SM'] = array(
        '#type' => 'textfield',
        '#title' => 'S/M',
        '#size' => 32,
        '#maxlength' => 32,
        '#required' => TRUE,
        '#default_value' => !empty($record) ? $record->SM:'',
    );
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => isset($record) ? '修改' : '添加',
//        '#access' =>nonghua_user_access('product_sku', 'edit', $id, !empty($record) ? $record->author_uid:0, 0) ,
        '#validate'=>array('product_sku_edit_form_validate'),

    );
    return $form;
}

function product_sku_edit_form_validate($form, &$form_state) {
    $id = isset($form_state['build_info']['args'][0]) ? $form_state['build_info']['args'][0] : 0;
    if(empty($form_state['values']['brand'])){
        form_set_error('brand','品牌不能为空。');
    }
    if(empty($form_state['values']['variety_name'])){
        form_set_error('variety_name','化肥名称不能为空。');
    }
    if(empty($form_state['values']['proportion_one'])){
        form_set_error('proportion_one','化肥比例不能为空。');
    }
    if(empty($form_state['values']['proportion_two'])){
        form_set_error('proportion_two','化肥比例不能为空。');
    }
    if(empty($form_state['values']['proportion_three'])){
        form_set_error('proportion_three','化肥比例不能为空。');
    }
    if(empty($form_state['values']['SM'])) {
        form_set_error('SM', '化肥S/M不能为空。');
    }else{
        $rule  = "/^\([A-Z]{1,}\)$/";
        preg_match($rule,$form_state['values']['SM'],$result);
        if(empty($result)){
            form_set_error('SM','SM书写格式不正确,正确格式如：(MOP)。小括号为英文括号，字母大写');
        }
    }
    $query = db_select('product_sku', 'ps');
    $query->condition('ps.brand_id', $form_state['values']['brand']);
    $query->condition('ps.variety_name', db_like($form_state['values']['variety_name']), 'LIKE');
    $query->condition('ps.proportion', db_like($form_state['values']['proportion_one'] . '-' . $form_state['values']['proportion_two'] . '-' . $form_state['values']['proportion_three']), 'LIKE');
    $query->condition('ps.SM', db_like($form_state['values']['SM']), 'LIKE');
    $query->condition('ps.product_sku_id', $id, '<>');
    $query->fields('ps');
    $record = $query->execute()->fetchAssoc();
    if (!empty($record)) {
        form_set_error('', "该信息已存在");

    }
}

function product_sku_edit_form_submit($form, &$form_state) {
    global $user;
    $id = isset($form_state['build_info']['args'][0]) ? $form_state['build_info']['args'][0] : NULL;
    if (empty($id)) { //add new product_sku
        $nid = db_insert('product_sku') // Table name no longer needs {}
        ->fields(array(
            'brand_id' => $form_state['values']['brand'],
            'variety_name' => $form_state['values']['variety_name'],
            'proportion' => $form_state['values']['proportion_one'].'-'.$form_state['values']['proportion_two'].'-'.$form_state['values']['proportion_three'],
            'SM' =>$form_state['values']['SM'],
            'author_uid' => $user->uid,
            'timestamp' => time(),))
            ->execute();
        if ($nid > 0) {
            drupal_set_message(t('添加成功！'), 'status', FALSE);
        }
//        drupal_goto('product_sku/list');
    } else {
        $nid = db_update('product_sku') // edit exsting store. todo: update only modified fields
        ->fields(array(
            'brand_id' => $form_state['values']['brand'],
            'variety_name' => $form_state['values']['variety_name'],
            'proportion' => $form_state['values']['proportion_one'].'-'.$form_state['values']['proportion_two'].'-'.$form_state['values']['proportion_three'],
            'SM' => $form_state['values']['SM'],
//            'author_uid' => $user->uid,
            'timestamp' => time(),))
            -> condition('product_sku_id',$id, '=')
            ->execute();
        if ($nid > 0) {
            drupal_set_message(t('修改成功！'), 'status', FALSE);
        }

//        drupal_goto('product_sku/list');
    }
}